package org.example.task;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
 * 每秒钟输出一次当前时间的任务
 */
public class TimePrintTask implements Runnable {
    private final String taskName;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public TimePrintTask(String taskName) {
        this.taskName = taskName;
    }

    @Override
    public void run() {
        System.out.println("任务 [" + taskName + "] 开始执行...");
        try {
            for (int i = 0; i < 10; i++) {
                Date now = new Date();
                System.out.println("任务 [" + taskName + "] - 当前时间: " + sdf.format(now));
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.out.println("任务 [" + taskName + "] 被中断");
        }
        System.out.println("任务 [" + taskName + "] 执行完毕！");
    }
}
